home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.12 Dec 96 / Async I⁄O Code / Sources / ResorcererDialogLib.c < prev    next >
Encoding:
Text File  |  1996-02-24  |  7.1 KB  |  297 lines  |  [TEXT/CWIE]

  1. // Dialog library, created by Resorcerer and modified by RDC
  2.  
  3. /*
  4.  *    Center a given window, w, horizontally in the main screen, top pixels from
  5.  *    the top, or centered vertically if top is 0.  The window should be invisible.
  6.  */
  7.  
  8. #include "ResorcererDialogLib.h"
  9.  
  10.  
  11. void CenterWindow(WindowPtr w, int top)
  12.     {
  13.         Rect scr; Point p;
  14.         int rsize,size,margin,xoff,yoff;
  15.  
  16.         scr = qd.screenBits.bounds;
  17.         SetPort(w);
  18.         p.h = w->portRect.left; p.v = w->portRect.top;
  19.         LocalToGlobal(&p);
  20.  
  21.         size = scr.right - scr.left;
  22.         rsize = w->portRect.right - w->portRect.left;
  23.         margin = size - rsize;
  24.         if (margin > 0) {
  25.             margin >>= 1;
  26.             p.h = scr.left + margin;
  27.             }
  28.         size = scr.bottom - scr.top;
  29.         rsize = w->portRect.bottom - w->portRect.top;
  30.         margin = size - rsize;
  31.         if (margin > 0) {
  32.             margin >>= 1;
  33.             p.v = scr.top + margin;
  34.             }
  35.         MoveWindow(w,p.h,top?top:p.v,FALSE);
  36.     }
  37.  
  38. /* Local C string length routine */
  39.  
  40. static long strlen(register char *str)
  41.     {
  42.         register char *p;
  43.  
  44.         p = str;
  45.         while (*p++) ;
  46.         return((long)(--p - str));
  47.     }
  48.  
  49. /* Convert in place a Pascal string to C string, and deliver its address */
  50.  
  51. char *PascalToC(char *str)
  52.     {
  53.         register char *p,*q,*end;
  54.  
  55.         end = str + *(unsigned char *)str;
  56.         q = (p=str) + 1;
  57.         while (p < end) *p++ = *q++;
  58.         *p = '\0';
  59.         return(str);
  60.     }
  61.  
  62. /*
  63.  *    Convert in place a C string to Pascal string, and deliver its address.
  64.  *    The C string should not be greater than 255 chars in length, or the
  65.  *    resulting Pascal string will be truncated to 255 chars.
  66.  */
  67.  
  68. char *CToPascal(char *str)
  69.     {
  70.         register char *p,*q;
  71.         register long len;
  72.  
  73.         len = strlen(str);
  74.         if (len > 255) len = 255;
  75.         p = str + len;
  76.         q = p-1;
  77.         while (p != str) *p-- = *q--;
  78.         *str = len;
  79.         return(str);
  80.     }
  81.  
  82. /* Dialog Item Stuffers */
  83.  
  84. /*
  85.  *    Install a given Pascal string, str, into the given static or edit text item
  86.  *    in the dialog, dlog.  If the item is an edit text item, leave the installed
  87.  *    text selected or not according to the value of sel (TRUE or FALSE).
  88.  */
  89.  
  90. void PutDlgString(DialogPtr dlog, int item, char *str, int sel)
  91.     {
  92.         short type; Handle hndl; Rect box;
  93.  
  94.         GetDItem(dlog,item,&type,&hndl,&box);
  95.         SetIText(hndl,(void*)str);
  96.         if (type == editText)
  97.             SelIText(dlog,item,sel?0:32767,32767);
  98.         InvalRect(&box);
  99.     }
  100.  
  101. /*
  102.  *    Install a given decimal long value into the static or edit text item of the
  103.  *    given dialog, dlog.  If the item is an edit text item, leave the installed
  104.  *    text for the number selected or not according to sel (TRUE or FALSE).
  105.  */
  106.  
  107. void PutDlgLong(DialogPtr dlog, int item, long val, int sel)
  108.     {
  109.         char str[32];
  110.  
  111.         NumToString(val,(void*)str);
  112.         PutDlgString(dlog,item,str,sel);
  113.     }
  114.  
  115. /*
  116.  *    Same as above, only for an int (word) decimal number.
  117.  */
  118.  
  119. void PutDlgWord(DialogPtr dlog, int item, int val, int sel)
  120.     {
  121.         PutDlgLong(dlog,item,(long)val,sel);
  122.     }
  123.  
  124. /*
  125.  *    Set the given check box or radio button item of the given dialog, dlog, to
  126.  *    on or off, according to val.
  127.  */
  128.  
  129. void PutDlgChkRadio(DialogPtr dlog, int item, int val)
  130.     {
  131.         short type; Handle hndl; Rect box;
  132.  
  133.         GetDItem(dlog,item,&type,&hndl,&box);
  134.         SetCtlValue((ControlHandle)hndl,val!=0);
  135.     }
  136.  
  137. /*
  138.  *    Deliver the value of the checkbox or radio button item of the given dialog.
  139.  */
  140.  
  141. int GetDlgChkRadio(DialogPtr dlog, int item)
  142.     {
  143.         short type; Handle hndl; Rect box;
  144.         
  145.         GetDItem(dlog,item,&type,&hndl,&box);
  146.         return(GetCtlValue((ControlHandle)hndl) != 0);
  147.     }
  148.  
  149. /* Dialog Item Unstuffers */
  150.  
  151. /*
  152.  *    Retrieve the value of an edit text item in a given dialog, placing the
  153.  *    resulting Pascal string in the buffer, str, which is assumed large enough
  154.  *    to hold the text (256 bytes max).  If item is the number of a static text
  155.  *    item, the empty string is delivered.  Delivers TRUE or FALSE according to
  156.  *    whether or not the text so delivered was empty.  
  157.  */
  158.  
  159. int GetDlgString(DialogPtr dlog, int item, char *str)
  160.     {
  161.         short type; Handle hndl; Rect box;
  162.  
  163.         GetDItem(dlog,item,&type,&hndl,&box);
  164.         if (type == editText) GetIText(hndl,(void*)str);
  165.          else                 *str = 0;
  166.         return(*str != 0);
  167.     }
  168.  
  169. /*
  170.  *    Retrieve the value of an edit text item in a given dialog, converting the
  171.  *    Pascal string to a long and setting *val to it.  Delivers TRUE or FALSE
  172.  *    according to whether or not the text so delivered was empty.  If FALSE,
  173.  *    *val is set to 0; if TRUE, *val is set to whatever StringToNum() says the
  174.  *    value is, even if the text contains non-numerical characters.
  175.  */
  176.  
  177. int GetDlgLong(DialogPtr dlog, int item, long *val)
  178.     {
  179.         int ans; char str[256];
  180.  
  181.         *val = 0;
  182.         if (ans = GetDlgString(dlog,item,str)) {
  183.             // Check that we do indeed have a valid number
  184.             int count;
  185.             
  186.             for (count = *str; count >= 1; count--) {
  187.                 char currChar = str[count];
  188.                 if ((currChar < '0') || (currChar > '9'))
  189.                     return false;
  190.             }
  191.             StringToNum((void*)str,val);
  192.         }
  193.         return(ans);
  194.         }
  195.  
  196. /* Same as above, only delivers the value of a word */
  197.  
  198. int GetDlgWord(DialogPtr dlog, int item, short *val)
  199.     {
  200.         int ans; long num;
  201.  
  202.         *val = 0;
  203.         if (ans = GetDlgLong(dlog,item,&num))
  204.             *val = num;
  205.         return(ans);
  206.     }
  207.  
  208. /*
  209.  *    Deliver the number of the current editText item in given dialog if any text
  210.  *    is selected in it, or 0 if none selected.
  211.  */
  212.  
  213. int TextSelected(DialogPtr dlog)
  214.     {
  215.         register TEHandle textH; int item = 0;
  216.         
  217.         textH = ((DialogPeek)dlog)->textH;
  218.         if (*textH)
  219.             if ( (*textH)->selStart != (*textH)->selEnd )
  220.                 item = ((DialogPeek)dlog)->editField+1;
  221.         return(item);
  222.     }
  223.  
  224. /*
  225.  *  If any of the variable argument scrap types are available for pasting from
  226.  *  the scrap, deliver the first one.  Otherwise, deliver 0.  For example,
  227.  *    
  228.  *      if (whichType = CanPaste(3,'TEXT','PICT','STUF')) ...
  229.  *
  230.  *  There can be any number of types in the list, as long as the preceding count, n,
  231.  *  is correct.
  232.  */
  233.  
  234. OSType CanPaste(int n, ...)
  235.     {
  236.         register OSType nextType,ans = 0L;
  237.         long err,offset;
  238.         va_list nextArg;
  239.         
  240.         va_start(nextArg,n);
  241.         nextType = va_arg(nextArg, OSType);
  242.         
  243.         while (n-- > 0) {
  244.             err = GetScrap(NIL, nextType, &offset);
  245.             if (err >= -1) {
  246.                 ans = nextType;
  247.                 break;
  248.                 }
  249.             nextType = va_arg(nextArg, OSType);
  250.             }
  251.         
  252.         va_end(nextArg);
  253.         return(ans);
  254.     }
  255.  
  256. /*
  257.  *    Frame or unframe a default dialog item (presumed a button) in given dialog.
  258.  *    Note that you don't need to use an extra user item to do this unless you
  259.  *    are doing some sort of non-standard default highlighting (not recommended).
  260.  */
  261.  
  262. void FrameDefault(DialogPtr dlog, int item, int frame)
  263.     {
  264.         short type; Handle hndl; Rect box;
  265.         GrafPtr oldPort; PenState oldPen;
  266.         
  267.         GetPort(&oldPort); SetPort(dlog);
  268.         GetPenState(&oldPen);
  269.         
  270.         GetDItem(dlog,item,&type,&hndl,&box);
  271.         InsetRect(&box,-4,-4);
  272.         
  273.         PenSize(3,3);
  274.         if (frame) PenPat(&qd.black);        /* Paint frame */
  275.          else      PenPat(&qd.white);        /* Erase frame */
  276.         FrameRoundRect(&box,16,16);
  277.         
  278.         SetPenState(&oldPen);
  279.         SetPort(oldPort);
  280.     }
  281.  
  282. /*
  283.  *    Get rectangle, *panel, for a given item (usually a user or picture item)
  284.  *    and then hide the item so that it doesn't interfere with mouse clicking.
  285.  *    This lets you stop worrying about the item order any user or pict items that
  286.  *    obscure other items in the item list, which can affect how the DialogMgr
  287.  *    returns itemHits.
  288.  */
  289.  
  290. void GetDlgPanel(DialogPtr dlog, int item, Rect *panel)
  291.     {
  292.         short type; Handle hndl;
  293.         
  294.         GetDItem(dlog,item,&type,&hndl,panel);
  295.         HideDItem(dlog,item);
  296.     }
  297.